package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/7 15:16
 */
public class M0106 {


    // https://leetcode-cn.com/problems/compress-string-lcci/
    // 暴力解, 一次循环计数拼接, 返回
    public static String compressString(String S) {
        if(S.equals("")) {
            return S;
        }
        StringBuilder sb = new StringBuilder();
        char[] str = S.toCharArray();
        int count = 1;
        for (int i = 0; i < str.length-1; i++) {
            if(str[i] != str[i+1]) {
                sb.append(str[i]).append(count);
                count = 1;
                continue;
            }
            count++;
        }
        /*if(count != 1) {
            sb.append(S.charAt(S.length()-1)).append(count);
        }*/
        sb.append(S.charAt(S.length()-1)).append(count);
        return sb.length() >= S.length()? S: sb.toString();
    }



    // for test
    public static void main(String[] args) {
        String str = "abbccd";
        System.out.println(compressString(str));
    }

}
